//
//  LZMyReviewedTableView.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/11/15.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyReviewedTableView.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "UITableView+LZTableView.h"
#import "LAReviewTableViewCell.h"
#import "LAReviewModel.h"
#import "LZNoDataPublicView.h"

@interface LZMyReviewedTableView ()<UITableViewDelegate,UITableViewDataSource,LAReviewCellDelegate>

@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, assign) int page;
@property (nonatomic, assign) int limit;
@end

@implementation LZMyReviewedTableView

- (instancetype)initWithType:(ReviewType)type {
    self = [self initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    
    self.reviewType = type;
    self.separatorColor = [UIColor clearColor];
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.backgroundColor = UIColorFromRGB(0xeeeeee, 1);

    [self registerCell];
    [self configBaseData];
    
    self.delegate = self;
    self.dataSource = self;
    
    return self;
}

- (void)checkHaveData {
    if (self.dataArr.count == 0) {
        if (self.subTableViewDelegate && [self.subTableViewDelegate respondsToSelector:@selector(requestDateWithTag:andPrameter:andPage:andLimit:resultSuccess:)]) {
            if (self.mj_footer.state != MJRefreshStateRefreshing) {
                [self.mj_footer beginRefreshing];
            }
        }
    }
}

#pragma mark - configure Super Refresh method

- (void)upLoadMoreData {
    [self requestList];
}

- (void)requestList {

    WS(weakSelf)
    
    [self.subTableViewDelegate requestDateWithTag:self.reviewType andPrameter:nil andPage:self.page andLimit:self.limit resultSuccess:^(NSArray *result,NSString *timestamp) {
        
        [weakSelf.mj_footer endRefreshing];
        
        if (result) {
            weakSelf.page ++;
            
            if (result.count>0) {
                [weakSelf.dataArr addObjectsFromArray:result];
                [weakSelf reloadData];
            }
            else {
                if (weakSelf.dataArr.count == 0) {
                    [LZNoDataPublicView showNoDataViewInView:weakSelf];
                    [weakSelf setNullStringFooterText];
                }
                else {
                    [LZNoDataPublicView hiddenNoDataViewInView:weakSelf];
                    [weakSelf setNoMoreDataFooterText];
                }
                
                [weakSelf.mj_footer endRefreshingWithNoMoreData];
            }
        }
    }];
}

#pragma mark - Private Method

- (void)registerCell
{
    self.estimatedRowHeight = 44.0;
    self.rowHeight = UITableViewAutomaticDimension;
    
    [self registerNib:[UINib nibWithNibName:@"LAReviewTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

- (void)configBaseData {
    self.page = 1;
    self.limit = 10;
    self.dataArr = [NSMutableArray array];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LAReviewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [self configureCell:cell atIndexPath:indexPath];
    
    return cell;
}

- (void)configureCell:(LAReviewTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    cell.fd_enforceFrameLayout = NO; // Enable to use "-sizeThatFits:"
    
    cell.indexPath = indexPath;
    cell.delegate = self;
    cell.reviewType = self.reviewType;
    if (self.reviewType == ReviewType_Restaurants) {
        cell.headImageType = HeadImageType_Restaurants;
    }
    else if (self.reviewType == ReviewType_Video ) {
        cell.headImageType = HeadImageType_Video;
    }
    
    cell.entity = self.dataArr[indexPath.section];
}

#pragma mark - UITableViewDelegate

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    LAReviewModel *model = self.dataArr[indexPath.section];
//    
//    float height = [tableView fd_heightForCellWithIdentifier:@"cell" cacheByIndexPath:indexPath configuration:^(LAReviewTableViewCell *cell) {
//        
//        [cell getReviewLabel].numberOfLines = model.labReviewNumberOfLine;
//        
//        [self configureCell:cell atIndexPath:indexPath];
//    }];
//    
//    return height;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)refreshCellHeight:(NSIndexPath *)indexPath {
    [self reloadData];
}

@end
